package com.ys.controller.web.purchase;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.purchase.TcgPurchaseContract;
import com.ys.service.flow.YspTaskService;
import com.ys.service.purchase.TcgPurchaseContractService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购合同管理
 */
@Controller
@RequestMapping("/web/purchase/tcgPurchaseContractByManage")
public class WebTcgPurchaseContractByManageController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcgPurchaseContractByManageController.class);

	@Autowired
	private CommonApiService<TcgPurchaseContract> commonApiService;

    private final Long MENU_ID = MenuId.TCG_PurchaseContract_ByManage;


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,Long itemId) {
        try {
            Map<String, Object> extraParam = getExtraParam(itemId);
            return commonApiService.toDetail(model,Long.parseLong(extraParam.get("id").toString()), new TcgPurchaseContract(), MENU_ID, extraParam, null);
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    public Map<String,Object> getExtraParam(Long itemId){
        Map<String,Object> extraParam = new TcgPurchaseContract()

                .addLeftJoin("YS_ProcInst P ON P.Bid = A.Bid ")
                .addField("FORMAT(P.UpdateTime, 'yyyy-MM-dd HH:mm:ss') AS auditingTime")//审核时间

                //.addLeftJoin("TCG_PurchaseAppointOrder O ON O.Id = A.PurchaseAppointOrderId ")//预约采购管理表
                .addLeftJoin("TB_CompanyInfo CI ON CI.Vid = A.CompanyInfoVid ")//公司信息
                .addField("CI.Vname AS vname")//购买方公司名称
                .addField("CI.Tax AS tax")//公司税号

                .addLeftJoin("TCW_PartnerBankInfo PB ON PB.Id = A.PartnerBankInfoId ")//合作商银行账户管理表
                .addField("PB.PartnerName AS partnerName")//供应方公司名称 、合作方名称
                .addField("PB.Vgssh AS vgssh")//供应方公司税号 、 合作方税号
                .addField("(PB.Vlxr + '&' +  PB.Vgsdh) AS vgsdh")//联系人及方式 、 联系人&联系方式
                .addField("PB.Vyhzhmc AS pbVyhzhmc")//收款户名 、 银行账户名称
                .addField("PB.Vkhh AS pbVkhh")//收款银行 、 开户行
                .addField("PB.Vyhzh AS pbVyhzh")//收款银行账户 、 银行账户编码

                .addLeftJoin("TCG_PurchaseContractItem S ON S.Pbid = A.Bid")//合同单子表
                .addField("S.Id AS itemId")//子表id
                .addField("S.SubBomNumber AS subBomNumber")//子件BOM数量
                .addField("S.SubPurchaseNum AS subPurchaseNum")//子件采购数量
                .addField("S.SubPurchasePrice AS subPurchasePrice")//子件采购单价
                .addField("S.PurchaseTotalPrice AS purchaseTotalPrice")//子件采购总价

                .addField("S.SubPaymentNum AS subPaymentNum")//子件付款数量
                .addField("S.SubWarehousingNum AS subWarehousingNum")//子件入库数量
                .addField("S.SubWriteOffNum AS subWriteOffNum")//子件核销数量
                .addField("S.SubPaymentState AS subPaymentState")//采购付款完整性（0否、1是）
                .addField("S.SubWarehousingState AS subWarehousingState")//采购入库完整性（0否、1是）
                .addField("S.SubWriteOffState AS subWriteOffState")//采购核销完整性（0否、1是）
                .addField("S.CancelState AS cancelState")//采购取消状态（0取消、1正常）

                .addField("CASE WHEN S.SubPaymentState = 0 THEN '否' ELSE '是' END subPaymentStateTxt")
                .addField("CASE WHEN S.SubWarehousingState = 0 THEN '否' ELSE '是' END subWarehousingStateTxt")
                .addField("CASE WHEN S.SubWriteOffState = 0 THEN '否' ELSE '是' END subWriteOffStateTxt")
                .addField("CASE WHEN S.CancelState = 0 THEN '取消' ELSE '正常' END cancelStateTxt")


                .addLeftJoin("TCG_PurchaseAppointOrderProductComponent C ON C.Id = S.OrderProductComponentId")//预约采购管理子件表
                .addLeftJoin("TCG_PurchaseAppointOrderItem PC ON C.Pid = PC.Id")//预约采购管理母件表
                .addLeftJoin("TCG_PurchaseAppointOrder PPC ON PPC.Bid = PC.Pbid")//预约采购单母表
                .addLeftJoin("THR_OperateCompanyApply OCA ON OCA.Vgsbm = PPC.Vgsbm")//运营公司管理表


                .addField("PPC.Bid AS appointOrderBid")//预约采购单单号
                .addField("PPC.Vgsbm AS vgsbm")//运营公司编码
                .addField("OCA.Vgsjc AS vgsjc")//运营公司简称
                .addField("PC.ProductCode AS productCode")//商品编码
                .addField("PC.ProductName AS productName")//商品名称
                .addField("PC.CodeMerge AS codeMerge")//合并商品编码
                .addField("PC.ReserveNumber AS reserveNumber")//母件预约数量
                .addField("C.SubCode AS subCode")//子件编码
                .addField("C.SubName AS subName")//子件名称
                .addField("C.SubCodeMerge AS subCodeMerge")//子件合并编码
                //.addField("C.SubBomNumber AS subBomNumber")//子件BOM数量
                //.addField("C.SubBomPrice AS subBomPrice")//子件BOM单价
                .addField("C.SubReserveNumber AS subReserveNumber")//子件预约数量


                .where("S.Id = "+itemId)
                .queryFirstMap();

        return extraParam;
    }


}






